/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.info.service.plugins  
 */
package com.apache.info.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.info.manager.InfoReleaseManager;
import com.apache.util.ConfigUtil;

/**
 * description:  验证表是否存在
 * @author Hou Dayu 创建时间：2015-8-28  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class ExistTablePluiginImpl implements PluginConnector {

	private InfoReleaseManager infoReleaseManager;

	/**
	 * TODO 验证表是否存在插件实现.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo vo) throws Exception {
		String tableName = String.valueOf(vo.getParams("tableName"));
		long tbNum = 0;
		if (Validator.isNotNull(tableName)) {
			ParamsVo pvo = new ParamsVo();
			pvo.setParams("dyncSql", getSqlStr(tableName));
			pvo.setMethodKey("ExistTable");
			tbNum = infoReleaseManager.countInfo(pvo);
		}
		return tbNum;
	}

	private String getSqlStr(String tableName) {
		String dbType = ConfigUtil.getInstance().getValueByKey("jdbc.properties", "database_type");
		//myssql,oracle,sqlserver,db2
		String sql = "";
		if ("oracle".equals(dbType)) {
			sql = "select count(*) from tabs where table_name ='" + tableName.toUpperCase() + "'";
		} else if ("sqlserver".equals(dbType)) {
			sql = "select count(1) from sys.systables where LCASE(tablename)='pub_metadata'" + tableName + "'";
		} else if ("myssql".equals(dbType)) {
			sql = "select count(1) from INFORMATION_SCHEMA.TABLES where TABLE_NAME='" + tableName + "'";
		}
		return sql;
	}

	public void setInfoReleaseManager(InfoReleaseManager infoReleaseManager) {
		this.infoReleaseManager = infoReleaseManager;
	}

}
